package com.sgu6.serialport.utils;

public class CheckTool {

    public static String checkCmdForMicro(String cmd) {
        try {
            if (cmd.length() < 10) {
                return null;
            } else {
                int start = cmd.indexOf("5AA5");
                int dataLength = Integer.parseInt(cmd.substring(start + 4, start + 6), 16);
                int end = 2 * dataLength + 6;
                if (cmd.length() >= end + start) {
                    return cmd.substring(start, end + start);
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            return null;
        }

    }

    public static String checkCmdForCard(String cmd) {
        try {
            if (cmd.length() > 2) {
                int start = cmd.indexOf("AA");
                int dataLength = Integer.parseInt(cmd.substring(start + 2, start + 4), 16);
                int end = 2 * dataLength + 4;
                if (cmd.length() >= end + start) {
                    return cmd.substring(start, end + start);
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }


    }
}
